// pages/registorpage/index.js
var app=getApp();
var Mcaptcha = require('../../utils/mcaptcha.js');
import WxValidate from '../../utils/WxValidate';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sign:'',
    sex:'',
    avatar:'',
    wxOpenid:'',
    userName:'',
    mobile:'',
    code:'',
    validCode:'',
    iscode:null,
    validId:'',
    codename:'获取验证码',
    identity:'',
    accept:'',
    password:'',
    imgCode:'',
    provinces:[],
    municipals:[],
    districtCountys:[],
    offices:[],
    currProvince:'',
    currMunicipal:'',
    currDistrictCounty:'',
    smallLocalBranch:'',
    currOffice:'',
    show: false,
    show2:false,
    show3:false,
    show4:false,
     index:-1,
     index2:-1,
     index3:-1,
    index4: -1,
    otherShow:false,
    inputOff:false,
    districtCountysShow:false,
    qtcompany:false,
    inputOffices:"",
    buttonClicked:false
  },
  showModal(error) {
    wx.showToast({
      title: error.msg,
      duration: 2000,
      icon: 'none'
    });   
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initValidate();
    var _this=this;
    app.paiRequest("/app/sys/office/findList", { parentCode: 0 }).then(function (res) {
      _this.setData({
        provinces:res.data,
        sex: options.gender,
        sign: options.sign,
        avatar: options.avatarUrl
      })
    });  
  },
  wxShowModal: function (title, text) {
    wx.showToast({
      title: title,
      duration: 2000,
      icon: 'none'
    });   
  },
  initValidate(){
    const rules = {
      userName: {
        required: true,
        userName:true
      },
      mobile:{
        required:true,
        tel: true,
      },
      regcode:{
       required:true,
        minlength:4,
      },
      password: {
        required: true,
        minlength:6,
        maxlength:16
      }
    }
    const message = {
      userName: {
        required: '请输入真实姓名',
        rangelength: '请输入真实姓名'
      },
      mobile:{
        required: '请输入11位手机号码',
        tel: '手机号不能少于和大于11位',
      },
      regcode:{
        required: '请输校验码',
        minlength: '请输入正确的校验码',
      },
      password: {
        required: '请输密码',
        minlength: '密码长度不能少于6位',
        maxlength: '密码长度不能超过16位'
      },
    }
    this.WxValidate=new WxValidate(rules,message);
    this.WxValidate.addMethod('userName',(value,param)=>{
      return /^[\u4E00-\u9FA5]{2,4}$/.test(value)
    },'请输入真实姓名')
  },
  /**
   * 设置用户名
   */
  setSign:function(e){    
    var value = e.detail.value;         
      this.setData({
        userName:e.detail.value
      })   
  },
  /**
   * 设置验证码
   */
  codeImg:function(e){
    this.setData({
      imgCode:e.detail.value
    })
  },
   /**
    * 身份选择事件
    */
  roleChange:function(e){    
    var _this=this;
    _this.setData({
      identity:e.detail.value
    })
  },
  /**
   * 是否同意协议
   */
  accept:function(e){    
    var _this=this;
    _this.setData({
      accept: e.currentTarget.dataset.value
    })
    console.log(_this.data.accept);
  },
  /**
   * 设置密码
   */
  setPassword:function(e){
     this.setData({
       password:e.detail.value
     })
  },
  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.mcaptcha = new Mcaptcha({
      el: 'canvas',
      width: 80,
      height: 35,
      createCodeImg: ""
    });
  },
  //刷新验证码
  onTap() {
    this.mcaptcha.refresh();
  },
  validmyCode:function(){
    var res = this.mcaptcha.validate(this.data.imgCode);
    if (this.data.imgCode == '' || this.data.imgCode == null) {
      
      wx.showToast({
        icon:'none',
        title: '请输入验证码'
      })
      return false;
    }
    if (!res) {
      wx.showToast({
        icon: 'none',
        title: '验证码错误'
      })
      return false;
    }
    return true;
  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 手机号输入
   */
  getPhoneValue:function(e){
    this.setData({
      mobile:e.detail.value
    })
  },
  /**
   * 获取验证码
   */
  getVerificationCode:function(events) {
     this.getCode();
     var _this=this;
     _this.setData({
       disabled:true
     })
  },
  getCode:function() {
    var num = 61;
    var timer = setInterval(function () {
      num--;
      if (num <= 0) {
        clearInterval(timer);
        _this.setData({
          codename: '重新发送',
          disabled: false
        })
      } else {
        _this.setData({
          codename: num + "s"
        })
      }
    }, 1000);
     var a=this.data.mobile;
     var _this=this;
    var myreg = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
    if(this.data.mobile == ""){
      wx.showToast({
        title: '手机号不能为空',
        icon:'none',
        duration: 2000
      })
      return false;
    } else if(!myreg.test(this.data.mobile)){
      wx.showToast({
        title: '请输入正确的手机号',
        icon:'none',
        duration: 2000
      })
      return false;
    }else{      
      wx.request({
        data:{"mobile":this.data.mobile},
        url: app.data.service_url +'/app/valid/validcode',
        method:'GET',
        header:{'content-type':'application/x-www-form-urlencoded'},
        success:function(res){
          console.log(res);
          _this.setData({
            validId:res.data.data.id
          })          
        },
        fail:function(res){
          console.log(res);
        }
      })
    }
  },
  formSubmit:function(events){
    var vae = this.validmyCode();
    const params=events.detail.value;
    console.log(params);
    if(!this.WxValidate.checkForm(params)){
      const error=this.WxValidate.errorList[0]
      this.showModal(error);
      return;
    }
    
    if (!vae){
      this.mcaptcha.refresh();
      return;
    }
    wx.login({
      success: res => {
        console.log(res)
        var code = res.code;
        var sumit = {
          appid: 'wxd598d704f54aa600',
          secret: '09ff4e7f657eda6eecb4041d77fc58af',
          js_code: res.code,
          sign: this.data.sign,
          sex: this.data.sex,
          avatar: this.data.avatar,
          wxOpenid: this.data.wxOpenid,
          userName: this.data.userName,
          mobile: this.data.mobile,
          identity: this.data.identity,
          accept: this.data.accept,
          password: this.data.password,
          headOffice: this.data.currProvince,
          branchOffice: this.data.currMunicipal,
          subBranch: this.data.currDistrictCounty,
          smallLocalBranch: this.data.currOffice,
          inputOffices: this.data.inputOffices
        };
        this.setData({ buttonClicked: true });
        app.paiRequest("/app/user/register", sumit).then(function (res) {
          var data = res.data;
          getApp().globalData.userInfo = data;
          wx.showToast({
            title: "注册成功！",
            icon: 'none',
            duration: 2000,
            mask: true
          })
          
          wx.navigateTo({
            url: "/pages/mine/userinfo/index",
          })
          
        })
        this.setData({ buttonClicked: false });
      }
    })   
    this.mcaptcha.refresh();
  },
  selectTap:function(){
    this.setData({
      show:!this.data.show,
      show2: false,
      show3: false,
      show4: false,
    })
  },
  selectMunicipal:function(){
    this.setData({
      show2: !this.data.show2,
      show: false,
      show3: false,
      show4: false,
    })
  },
  selectDistrictCounty:function(){
    this.setData({
      show3: !this.data.show3,
      show: false,
      show2: false,
      show4: false,
    })
  },
  selectOffices:function(){
    this.setData({
      show: false,
      show2: false,
      show3: false,
      show4: !this.data.show4
    })
  },
  optionTap:function(e){
    let Index = e.currentTarget.dataset.index;//获取点击的下拉列表的下标  
    let id=e.currentTarget.dataset.id;
    var _this = this;
    if (id =='qtcompany'){
      _this.setData({
        index: Index,
        show: !_this.data.show,
        currProvince: id,
        qtcompany: true,
        districtCountysShow:false,
        municipalsShow: false,
        officesShow:false,
      })
    }else{
    app.paiRequest("/app/sys/office/findList", { parentCode: id }).then(function (res) {
      console.log(res);
      var arr=res.data;
        if(arr.length>0){
        _this.setData({
          index: Index,
          show: !_this.data.show,
          currProvince: id,
          municipalsShow:true,
          municipals: arr,
          qtcompany: false
        })
        }else{
          _this.setData({
            index: Index,
            show: !_this.data.show,
            currProvince: id,
            municipalsShow: false,
            qtcompany: false
          })
        }
    });  
    }  
  },
  optionMunicipal:function(e){
    let Index = e.currentTarget.dataset.index;//获取点击的下拉列表的下标  
    let id = e.currentTarget.dataset.id;
    var _this = this;
    app.paiRequest("/app/sys/office/findList", { parentCode: id }).then(function (res) {
      console.log(res);
      var arr = res.data;
      if(arr.length>0){
        _this.setData({
          index2: Index,
          show2: !_this.data.show2,
          districtCountys: arr,
          districtCountysShow:true,
          currMunicipal: id,
        })
      }else{
        _this.setData({
          index2: Index,
          show2: !_this.data.show2,
          districtCountysShow: false,
          currMunicipal: id,
        })
      }
    }); 
  },
  optionDistrictCounty:function(e){
    let Index = e.currentTarget.dataset.index;//获取点击的下拉列表的下标  
    let id = e.currentTarget.dataset.id;
    
    var _this=this;
    app.paiRequest("/app/sys/office/findList", {
      province: this.data.currProvince,
      municipal: this.data.currMunicipal,
      parentCode: id,
      }).then(function (res) {
       console.log(res);
       _this.setData({
         index3: Index,
         offices:res.data,
         show3: !_this.data.show3,
         currDistrictCounty: id,
       })
    });
    
  },
  optionOffices:function(e){
    let Index = e.currentTarget.dataset.index;//获取点击的下拉列表的下标  
    let id = e.currentTarget.dataset.id;
    this.setData({
      currOffice:id,
      show4: !this.data.show4,
      index4: Index,
    })
  },
 

  textareaInput:function(e){
    console.log(e);
  },
  inputOffices:function(e){
    console.log(e);
    this.setData({
      inputOffices:e.detail.value
    })
  },
  readagrement:function(){
    wx.navigateTo({
      url: 'agreement/index',
    })
  }
})